package com.threebrothers.i_video.test

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.threebrothers.i_video.R
import com.threebrothers.i_video.databinding.ActivityTestBinding
import com.threebrothers.lib_mvi.base.BaseActivity
import com.threebrothers.lib_mvi.base.BaseMVIActivity
import kotlinx.coroutines.launch

class TestActivity : BaseMVIActivity() {

    private lateinit var viewModel : MyViewModel
    val binding : ActivityTestBinding by lazy {
        ActivityTestBinding.inflate(layoutInflater)
    }

    override fun generateViewModel() {
        viewModel = ViewModelProvider(this)[MyViewModel::class.java]
    }

    override fun initData() {
        super.initData()
        lifecycleScope.launch {
            viewModel.intents.send(MyIntent.GetBannerData)
        }
    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

    override fun handleState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                viewModel.bannerDataStateFlow_.collect{
                    when(it){
                        is MyState.Loading ->{
                            showMsg("数据加载中")
                        }
                        is MyState.Success ->{
                            showMsg("数据成功$it")
                        }
                        is MyState.Error ->{
                            showMsg("数据加载中")
                        }
                        is MyState.Empty ->{
                            showMsg("数据加载中")
                        }
                    }
                }
            }
        }
    }


}